package cn.kone.remote.impl;

import cn.hutool.core.date.DateUtil;
import cn.kone.pojo.domain.*;
import cn.kone.remote.api.RuleFeignApi;
import cn.kone.remote.dto.param.OrderDto;
import cn.kone.remote.dto.param.ProfitsharingDto;
import cn.kone.remote.dto.result.OrderProfitsharingResult;
import cn.kone.remote.dto.result.OrderResult;
import cn.kone.result.JSONResult;
import cn.kone.service.*;
import cn.kone.utils.AssertUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;

/**
 * 计算价格的类
 */
@RestController
public class RuleFeignApiImpl implements RuleFeignApi {

    @Autowired
    private IChargeRuleStartService chargeRuleStartService;

    @Autowired
    private IChargeRuleReturnService chargeRuleReturnService;


    @Override
    public JSONResult<OrderResult> addOrder(OrderDto orderDto) {
        //时间格式转换为24小时
        int hour = DateUtil.hour(orderDto.getCreateTime(), true);


        /**
         * 根据传递的下单时间和出数据库中的规则时间进行比较
         * 然后查询出下单时间在什么规则范围内
         * 不同的时间对应的起步价格也是不一样的
         */
        ChargeRuleStart ruleTimeObj = chargeRuleStartService.getOne(
                new LambdaQueryWrapper<ChargeRuleStart>()
                        .le(ChargeRuleStart::getHourStart, hour)
                        .ge(ChargeRuleStart::getHourEnd, hour)
        );
        //校验是否为null,如果为null就抛出异常信息
        AssertUtil.isNotNull(ruleTimeObj, "没有此类计费规则~~~");

        /**
         * 计算在不同时间的起步价格和总价
         */
        BigDecimal amount = ruleTimeObj.getAmount(); //拿到当前下单时间的起步价

        //创建OrderResult对象用来接收订单账单的详情信息
        OrderResult orderResult = new OrderResult();


        //预估里程和数据库里的基础程进行比较
        if (orderDto.getExpectsMileage().compareTo(ruleTimeObj.getBaseMileage())==1) {
            /**
             * 先拿到超出的里程,计算超出基本里程的价格
             */
            BigDecimal exceedMileage = orderDto.getExpectsMileage().subtract(ruleTimeObj.getBaseMileage()); //超出的里程
            BigDecimal exceedAmount = exceedMileage.multiply(ruleTimeObj.getExceedEveryKmAmount()); //超出里程的金额

            orderResult.setExceedBaseMileage(exceedMileage);//超出里程
            orderResult.setExceedBaseMileageAmount(exceedAmount); //超出里程的价格

            amount = amount.add(exceedAmount); //超出金额+起步金额
            orderResult.setMileageAmount(amount); //超出金额+起步金额
        }

        //查询出返程计费规则对象
        ChargeRuleReturn returnTimeObj = chargeRuleReturnService.getById(1);
        if (orderDto.getExpectsMileage().compareTo(returnTimeObj.getFreeBaseReturnMileage())==1) {

            BigDecimal exceedReturnMileage = orderDto.getExpectsMileage().subtract(returnTimeObj.getFreeBaseReturnMileage());//超出返程里程
            BigDecimal exceedAmount = exceedReturnMileage.multiply(returnTimeObj.getExceedEveryKmAmount());//超出返程里程的金额

            orderResult.setExceedFreeBaseReturnMileage(exceedReturnMileage); //超出返程的里程
            orderResult.setReturnAmont(exceedAmount); //超出返程里程的价格

            //起步价+超出返程里程的价格 得到总价格
            amount = amount.add(exceedAmount);
        }


        orderResult.setExpectsAmount(amount);//预估金额总价
        orderResult.setBaseMileage(ruleTimeObj.getBaseMileage());//基础公里数
        orderResult.setBaseMileageAmount(ruleTimeObj.getAmount());//起步价

        orderResult.setExceedBaseMileageEveryKmAmount(ruleTimeObj.getExceedEveryKmAmount()); //超出基础里程每公里的价格
        orderResult.setFreeBaseReturnMileage(returnTimeObj.getFreeBaseReturnMileage()); //免费返程的公里数
        orderResult.setExceedBaseReturnEveryKmAmount(returnTimeObj.getExceedEveryKmAmount()); //超出免费返程公里数以后的每公里的价格

        return JSONResult.success(orderResult);
    }



    @Autowired
    private IProfitsharingRuleBaseService profitsharingRuleBaseService;
    @Autowired
    private IProfitsharingRuleDeductCancelService profitsharingRuleDeductCancelService;
    @Autowired
    private IProfitsharingRuleDeductComplaintService profitsharingRuleDeductComplaintService;
    @Override
    public JSONResult<OrderProfitsharingResult> calprofitsharing(ProfitsharingDto profitsharingDto) {
        ProfitsharingRuleBase base = profitsharingRuleBaseService.getById(1);
        AssertUtil.isNotNull(base, "没有这个基础分成比例");

        BigDecimal driverRatio = base.getDriverRatio();//司机基础分成比例0.75

        //取消计算↓
        ProfitsharingRuleDeductCancel cancel = profitsharingRuleDeductCancelService.getOne(
                new LambdaQueryWrapper<ProfitsharingRuleDeductCancel>().le(ProfitsharingRuleDeductCancel::getNumberFrom, profitsharingDto.getOrderCancelCount())
                        .ge(ProfitsharingRuleDeductCancel::getNumberTo, profitsharingDto.getOrderCancelCount()));

        //AssertUtil.isNotNull(cancel, "没有这个取消计算规则");

        if (cancel != null) {
            driverRatio = driverRatio.subtract(cancel.getDriverRatioDeduct());
        }

        //投诉计算↓
        ProfitsharingRuleDeductComplaint complaint = profitsharingRuleDeductComplaintService.getOne(
                new LambdaQueryWrapper<ProfitsharingRuleDeductComplaint>().le(ProfitsharingRuleDeductComplaint::getNumberFrom, profitsharingDto.getOrderComplaintCount())
                        .ge(ProfitsharingRuleDeductComplaint::getNumberTo, profitsharingDto.getOrderComplaintCount()));

        //AssertUtil.isNotNull(cancel, "没有这个投诉计算规则");

        if (complaint != null) {
            driverRatio = driverRatio.subtract(complaint.getDriverRatioDeduct());
        }

        OrderProfitsharingResult orderProfitsharingResult = new OrderProfitsharingResult();
        orderProfitsharingResult.setPlatformIncomeAmount(profitsharingDto.getOrderAmount().multiply(new BigDecimal("1").subtract(driverRatio)));//平台分账收入
        orderProfitsharingResult.setDriverIncomeAmount(profitsharingDto.getOrderAmount().multiply(driverRatio));//司机分账收入
        orderProfitsharingResult.setPlatformRate(new BigDecimal("1").subtract(driverRatio));//平台分账比例
        orderProfitsharingResult.setDriverRate(driverRatio);//司机分账比例

        return JSONResult.success(orderProfitsharingResult);
    }
}
